<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="dateRange"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        range-separator="~"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :editable="false"
        class="filter-item"
        @change="changeDate"
      />
      <el-select v-model="listQuery.operation" placeholder="操作类型" clearable class="filter-item" style="width: 120px;">
        <el-option v-for="item in operationOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-input v-model="listQuery.createUser" placeholder="操作用户" class="filter-item" clearable style="width: 200px;" />
      <el-input v-model="listQuery.content" placeholder="操作内容" class="filter-item" clearable style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="操作类型" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.operation | operationFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作内容" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作用户" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="IP" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-folder-opened" type="primary" @click="handleDetail(row)">详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <WeblogDetailForm :id="editId" ref="weblogDetailForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getSysWeblog } from '@/api/weblog';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import WeblogDetailForm from '@/views/sys/weblogDetail.vue';
import { parseTime } from '@/utils';

var that;

export default {
  name: 'SysWeblog',
  components: { Pagination, WeblogDetailForm },
  directives: { waves },
  filters: {
    operationFilter(item) {
      const operationKeyValue = that.operationOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return operationKeyValue[item];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        operation: null,
        createUser: '',
        content: '',
        beginTime: '',
        endTime: ''
      },
      dateRange: [],
      operationOptions: [{ label: '访问', key: 0 }, { label: '登录', key: 1 }, { label: '新增', key: 2 }, { label: '更新', key: 3 }, { label: '删除', key: 4 }, { label: '导出', key: 5 }, { label: '导入', key: 6 }],
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    that = this;
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 00:00:00');
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 23:59:59');
    this.listQuery.beginTime = this.dateRange[0] + ' 00:00:00';
    this.listQuery.endTime = this.dateRange[1] + ' 23:59:59';
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSysWeblog(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    changeDate(val) {
      if (val) {
        this.listQuery.beginTime = val[0];
        this.listQuery.endTime = val[1];
      } else {
        this.listQuery.beginTime = null;
        this.listQuery.endTime = null;
      }
    },
    handleDetail(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    cancelClose(data) {
      this.dialogEditFormVisible = false;
    }
  }
};
</script>
